#include <iostream>
#include <random>
#include "ThreadPool.h"
#include "MyLogic.h"

using namespace std;

std::random_device rd; //真实随机数产生器

std::mt19937 mt(rd()); //生成计算随机数mt;

std::uniform_int_distribution<int> dist(-1000, 1000);//生成-1000到1000之间的离散均匀分部数

auto rnd = std::bind(dist, mt);

//设置线程睡眠时间

void simulate_hard_computation() {
    std::this_thread::sleep_for(std::chrono::milliseconds(2000 + rnd()));
}

// 添加两个数字的简单函数并打印结果

void multiply(const int a, const int b) {
    simulate_hard_computation();
    const int res = a * b;
    std::cout << a << " * " << b << " = " << res << std::endl;
}

//添加并输出结果

void multiply_output(int & out, const int a, const int b) {
    simulate_hard_computation();
    out = a * b;
    std::cout << a << " * " << b << " = " << out << std::endl;

    int count = 0;
    while(1)
    {
        count++;
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
}

// 结果返回

int multiply_return(const int a, const int b) {
    simulate_hard_computation();
    const int res = a * b;
    std::cout << a << " * " << b << " = " << res << std::endl;
    return res;
}


// 打印字符串
string printfStr(const string &str, int num)
{
    printf("str: %s | num: %d\n", str.c_str(), num);
    return string("Hello" + str);
}


int main()
{
#if 0
    // 自定义逻辑类处理， 负责读取数据
    MyLogic *pMylogic = new (std::nothrow) MyLogic;
    pMylogic->start();
#endif

    // 创建3个线程的线程池
    ThreadPool pool(3);

    // 初始化线程池
    pool.init();

    // 提交乘法操作，总共30个
    for (int i = 1; i < 3; ++i)
    {
        for (int j = 1; j < 10; ++j)
        {
            pool.submit(multiply, i, j);
        }
    }

    // 使用ref传递的输出参数提交函数
    int output_ref;
    auto future1 = pool.submit(multiply_output, std::ref(output_ref), 5, 6);
    // 等待乘法输出完成
    future1.get();
    std::cout << "Last operation result is equals to " << output_ref << std::endl;

    // 使用return参数提交函数
    auto future2 = pool.submit(multiply_return, 5, 3);
    // 等待乘法输出完成
    int res = future2.get();
    std::cout << "Last operation result is equals to " << res << std::endl;
    std::cout <<endl;

    // 等待打印返回结果
    auto future3 = pool.submit(printfStr, string("ADFGHJHJ"), 888);
    string str = future3.get();
    std::cout << "===str: " << str << endl;

    //关闭线程池
    pool.shutdown();

    return 0;
}


#if 0
#include "ProduceConsume.h"

int intFunc(int x, string str)
{
    cout << "==x: " << x << " |str: " << str << endl;
    return 0;
}

void func(int num, std::function<int(int, string)> &f)
{
    (void)num;
    f(3, "123");
}

// 函数模板
template<typename T>
string toStr(T num) {
    return to_string(num);
}

template<typename T>
string vecToString(const vector<T> &vec, const function<string(decltype(vec.front()))> &toStrFunc)
{
    string str = "[";
    size_t sz = vec.size();
    for (size_t i = 0; i < sz; ++i) {
        str += toStrFunc(vec[i]);
        if (i != sz - 1) {
            str += ", ";
        }
    }
    return str += "]";
}



int main(void)
{
    ProduceConsume pc;
    pc.startThread();

    cout << endl;

    // 类型推导
    // 1. 数据类型
    int num = 8;
    decltype (num) newNum = 9;
    cout << "newNum: " << newNum << "\n";

    // 2. 函数调用
    decltype (intFunc(0, "1")) x = 0;
    cout << "x: " << x << "\n";
    cout << endl;

    std::function<int(int, string)> pFunc = intFunc;
    func(num, pFunc);
    cout << endl;

    vector<int> a{1,2,3,4,5,6,7,8,9};
    cout << vecToString(a, toStr<int>) <<endl;
    return 0;
}

#endif
